package com.ygsj.Utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.uhf.uhf.Common.Comm;
import com.uhf.uhf.UHF1.UHF1Function.SPconfig;
import com.ygsj.R;
import com.ygsj.app.Configurator;
import com.ygsj.ui.loader.Loader;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static com.uhf.uhf.Common.Comm.isQuick;
import static com.uhf.uhf.Common.Comm.isrun;
import static com.uhf.uhf.Common.Comm.lsTagList;
import static com.uhf.uhf.Common.Comm.lsTagList6B;
import static com.uhf.uhf.Common.Comm.tagListSize;

public class UHFUtils {
    /*单例模式*/
    private UHFUtils() {
    }

    ;

    private static class Hodler {
        private static final UHFUtils INSTANCE = new UHFUtils();
    }

    public static UHFUtils getInstance() {
        return Hodler.INSTANCE;
    }
    /*单例模式*/


    public void InitDevice(Context context, Handler connectH) {

        Comm.repeatSound = true;//盘点到重复标签是出声音
        Comm.app = (Application) Configurator.getInstance().getApplicationContext();
        Comm.spConfig = new SPconfig(context);//封装的share
        Comm.context = context;

        /*以下声音*/
        if (Comm.soundPool == null) {
            Comm.soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
            Comm.soundPool.load(context, R.raw.beep51, 1);
        }

        Comm.checkDevice();//判断设备型号，初始化时,必须先执行此项操作
        Comm.initWireless(Comm.app);
        Comm.connecthandler = connectH;


        if (Comm.moduleType != null)
            Comm.ConnectModule();
        else
            Comm.Connect();

        //Loader.stopLoading();
    }

    /*释放资源*/
    public void release() {
        try {
            if (isrun)
                Comm.stopScan();//停止标签扫描

            Comm.Exit();//退出
            Comm.powerDown();//下电
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return EpcID
     * @method getEpcstr
     * @description 获取卡号;
     * @date: 2019/1/30 17:52
     * @author: lenovo
     */
    //public  String getEpcstr(Context context,boolean isClean){
    public String getEpcstr(Context context) {
        String epcstr = "";//epc
        try {
            // int ReadCnt = 0;//个数
            if (tagListSize > 0) {//不用循环是因为需求只要求展示一个即可
                // if (tagListSize > 1)
                // Toast.makeText(context, "请持卡人保持距离", Toast.LENGTH_SHORT).show();

                if (!isQuick || !isrun) {//实时展示模式,不管是不是运行都能进入
                    //Loader.showLoading(context);

                    if (Comm.is6BTag) {
                        epcstr = lsTagList6B.get(0).strUID;//刷第一张卡
                        if (epcstr != null && !epcstr.equals("")) {
                            epcstr = lsTagList6B.get(0).strUID.replace(" ", "");
                        }
                    } else {//大宁采用EPC标准;
                        epcstr = lsTagList.get(0).strEPC;
                        if (Comm.dt == Comm.DeviceType.supoin_JT) {
                            Comm.mWirelessMg.writeTo433(epcstr + "\n");
                        }

                        if (epcstr != null && !epcstr.equals("")) {
                            epcstr = lsTagList.get(0).strEPC.replace(" ", "");//控死只刷第一张卡
                            if (epcstr.length() > 24) {
                                epcstr = epcstr.substring(0, 24) + "\r\n" + epcstr.substring(24);
                            }
                            //Log.e("CardID", "Epcstr: " + epcstr);
                        }

                    }
                    // Loader.stopLoading();
                }
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (!isrun) {//不运行时候处理;
            //Toast.makeText(context, R.string.wait_operate, Toast.LENGTH_SHORT).show();
            Log.e("HHFUtils", "getEpcstr: " + "waiting...");
        }
        return epcstr;
    }

    /*清空数据*/
    public void clean() {
        int i = Comm.lv.size();
        if (i > 0) Comm.lv.clear();
        Comm.clean();
    }

    /**
     * 检查是否连接到了网络
     */
    public boolean isNetworkAvailable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {// 当前网络是连接的
            if (info.getState() == NetworkInfo.State.CONNECTED) { // 当前所连接的网络可用
                return true;
            }
        }
        return false;
    }

    /*用baidu判断网络是否联通,内网不连外网的情况很多,不能判断*/
    public boolean isConnByHttp() {
        boolean isConn = false;
        URL url;
        HttpURLConnection conn = null;
        try {
            url = new URL("https://www.baidu.com");
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(1000 * 2);
            if (conn.getResponseCode() == 200) {
                isConn = true;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return isConn;
    }

    //以下方法本应该申请动态权限,并且getDeviceId()是废弃的在<6.0的机子上可以跑,而getImei()需要在8.0的机子上跑;
    //因为手持机PDA是android5.0,所以忽略动态权限的申请,而且用getDeviceId()这个旧方法;
    public  String getIMEI(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager)
                context.getSystemService(context.TELEPHONY_SERVICE);
             @SuppressLint("MissingPermission") String IMEI = telephonyManager.getDeviceId();
            return IMEI;
    }
}
